package com.heima.common.config;

import com.heima.common.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
@Slf4j
public class MessageConverterConfiguration extends WebMvcConfigurationSupport {

    /**
     * 日期格式标准化
     * @param converters 转换器们
     */
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters){
        log.info("消息转换器注册成功");
        // 创建一个消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

        // 设置对象转换器，可以将Java对象转为json字符串
        converter.setObjectMapper(new JacksonObjectMapper());

        // 将我们自己的转换器放入到 spring MVC 框架的容器中
        converters.add(0, converter);
    }
}
